<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是应用编程基本概念——开始应用编程学习前的一些基础学习的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV01-01-应用编程基本概念-01-基础知识">
<meta property="og:url" content="https://sumumm.github.io/post/9baa712e.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是应用编程基本概念——开始应用编程学习前的一些基础学习的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV03-%E5%BA%94%E7%94%A8%E5%BC%80%E5%8F%91/LV01-01-%E5%BA%94%E7%94%A8%E7%BC%96%E7%A8%8B%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5-01-%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/image-20240522072123250.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV03-%E5%BA%94%E7%94%A8%E5%BC%80%E5%8F%91/LV01-01-%E5%BA%94%E7%94%A8%E7%BC%96%E7%A8%8B%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5-01-%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/640-1712705584672-8.webp">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV03-%E5%BA%94%E7%94%A8%E5%BC%80%E5%8F%91/LV01-01-%E5%BA%94%E7%94%A8%E7%BC%96%E7%A8%8B%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5-01-%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/640-1712620170595-3.webp">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV03-%E5%BA%94%E7%94%A8%E5%BC%80%E5%8F%91/LV01-01-%E5%BA%94%E7%94%A8%E7%BC%96%E7%A8%8B%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5-01-%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/640.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV03-%E5%BA%94%E7%94%A8%E5%BC%80%E5%8F%91/LV01-01-%E5%BA%94%E7%94%A8%E7%BC%96%E7%A8%8B%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5-01-%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/image-20240403073828181.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV03-%E5%BA%94%E7%94%A8%E5%BC%80%E5%8F%91/LV01-01-%E5%BA%94%E7%94%A8%E7%BC%96%E7%A8%8B%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5-01-%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/641.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV03-%E5%BA%94%E7%94%A8%E5%BC%80%E5%8F%91/LV01-01-%E5%BA%94%E7%94%A8%E7%BC%96%E7%A8%8B%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5-01-%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/640-1716246922738-3.webp">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV03-%E5%BA%94%E7%94%A8%E5%BC%80%E5%8F%91/LV01-01-%E5%BA%94%E7%94%A8%E7%BC%96%E7%A8%8B%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5-01-%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/image-20240521073946105.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV03-%E5%BA%94%E7%94%A8%E5%BC%80%E5%8F%91/LV01-01-%E5%BA%94%E7%94%A8%E7%BC%96%E7%A8%8B%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5-01-%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/image-20240521075140856.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV03-%E5%BA%94%E7%94%A8%E5%BC%80%E5%8F%91/LV01-01-%E5%BA%94%E7%94%A8%E7%BC%96%E7%A8%8B%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5-01-%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/image-20240521075233548.png">
<meta property="article:published_time" content="2024-11-08T10:50:14.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:57.034Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="(ALPHA)LV03-应用开发">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV03-%E5%BA%94%E7%94%A8%E5%BC%80%E5%8F%91/LV01-01-%E5%BA%94%E7%94%A8%E7%BC%96%E7%A8%8B%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5-01-%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/image-20240522072123250.png">


<link rel="canonical" href="https://sumumm.github.io/post/9baa712e.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":"","permalink":"https://sumumm.github.io/post/9baa712e.html","path":"post/9baa712e.html","title":"LV01-01-应用编程基本概念-01-基础知识"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV01-01-应用编程基本概念-01-基础知识 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E5%B5%8C%E5%85%A5%E5%BC%8FLinux%E7%BB%84%E6%88%90"><span class="nav-text">一、嵌入式Linux组成</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E7%B3%BB%E7%BB%9F%E8%B0%83%E7%94%A8"><span class="nav-text">二、系统调用</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%86%85%E6%A0%B8%E6%80%81%E4%B8%8E%E7%94%A8%E6%88%B7%E6%80%81"><span class="nav-text">1. 内核态与用户态</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E4%BB%80%E4%B9%88%E6%98%AF%E7%B3%BB%E7%BB%9F%E8%B0%83%E7%94%A8%EF%BC%9F"><span class="nav-text">2. 什么是系统调用？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E4%B8%BA%E4%BB%80%E4%B9%88%E8%A6%81%E6%9C%89%E7%B3%BB%E7%BB%9F%E8%B0%83%E7%94%A8%EF%BC%9F"><span class="nav-text">3. 为什么要有系统调用？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E7%B3%BB%E7%BB%9F%E8%B0%83%E7%94%A8%E6%80%8E%E4%B9%88%E5%AE%9E%E7%8E%B0%E7%9A%84%EF%BC%9F"><span class="nav-text">4. 系统调用怎么实现的？</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1-strace%E5%91%BD%E4%BB%A4"><span class="nav-text">4.1 strace命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-%E4%BB%A5%E5%BA%93%E5%87%BD%E6%95%B0write%E4%B8%BA%E4%BE%8B"><span class="nav-text">4.2 以库函数write为例</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-3-%E5%BA%93%E5%87%BD%E6%95%B0%E6%89%A9%E5%B1%95%E6%B1%87%E7%BC%96%E5%AE%8F"><span class="nav-text">4.3 库函数扩展汇编宏</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-4-int-0x80%E4%B8%AD%E6%96%AD-%E8%B0%83%E7%94%A8%E5%AF%B9%E5%BA%94%E7%9A%84%E4%B8%AD%E6%96%AD%E5%A4%84%E7%90%86%E5%87%BD%E6%95%B0"><span class="nav-text">4.4 int 0x80中断 调用对应的中断处理函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-5-%E6%A3%80%E7%B4%A2%E7%B3%BB%E7%BB%9F%E8%B0%83%E7%94%A8%E5%87%BD%E6%95%B0%E8%A1%A8"><span class="nav-text">4.5 检索系统调用函数表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-6-%E6%9C%80%E7%BB%88%E6%89%A7%E8%A1%8Csys-write"><span class="nav-text">4.6 最终执行sys_write</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-7-%E6%80%BB%E7%BB%93"><span class="nav-text">4.7 总结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-%E5%86%85%E6%A0%B8%E6%80%81%E4%B8%8E%E7%94%A8%E6%88%B7%E6%80%81%E6%95%B0%E6%8D%AE%E4%BA%A4%E4%BA%92"><span class="nav-text">5. 内核态与用户态数据交互</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E5%BA%93%E5%87%BD%E6%95%B0"><span class="nav-text">三、库函数</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81%E6%A0%87%E5%87%86C%E8%AF%AD%E8%A8%80%E5%BA%93%E5%87%BD%E6%95%B0"><span class="nav-text">四、标准C语言库函数</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-GNU-C%E5%BA%93"><span class="nav-text">1. GNU C库</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-glibc%E7%89%88%E6%9C%AC%EF%BC%9F"><span class="nav-text">2. glibc版本？</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/9baa712e.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV01-01-应用编程基本概念-01-基础知识 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV01-01-应用编程基本概念-01-基础知识
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2024-11-08 18:50:14" itemprop="dateCreated datePublished" datetime="2024-11-08T18:50:14+08:00">2024-11-08</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/" itemprop="url" rel="index"><span itemprop="name">02IMX6ULL平台</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV03-%E5%BA%94%E7%94%A8%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">LV03-应用开发</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>8.7k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>31 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是应用编程基本概念——开始应用编程学习前的一些基础学习的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV03-%E5%BA%94%E7%94%A8%E5%BC%80%E5%8F%91/LV01-01-%E5%BA%94%E7%94%A8%E7%BC%96%E7%A8%8B%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5-01-%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" rowspan="5">PC端开发环境</td>        <td align="center" width=150px>Windows</td>        <td align="left">Windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu20.04.6的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 17 Pro</td>        <td align="left">17.0.0 build-20800274</td>      </tr>    <tr>        <td align="center">终端软件</td>        <td align="left">MobaXterm(Professional Edition v23.0 Build 5042 (license))</td>    </tr>    <tr>        <td align="center">Win32DiskImager</td>        <td align="left">Win32DiskImager v1.0</td>      </tr>    <tr>        <td align="center" rowspan="3">Linux开发板环境</td>        <td align="center">Linux开发板</td>        <td align="left">正点原子 i.MX6ULL Linux 阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，NXP提供的版本为uboot-imx-rel_imx_4.1.15_2.1.0_ga(使用的uboot版本为U-Boot 2016.03)</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.15(NXP官方提供)</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">分类</td>        <td align="center">网址</td>        <td align="center">说明</td>    </tr>    <tr>        <td align="center" rowspan="5">官方网站</td>        <td align="left"><a href="https://www.arm.com/" target="_blank">https://www.arm.com/</a></td>        <td align="left">ARM官方网站，在这里我们可以找到Cotex-Mx以及ARMVx的一些文档</td>    </tr>    <tr>        <td align="left"><a href="https://www.nxp.com.cn/" target="_blank">https://www.nxp.com.cn/ </a></td>        <td align="left">NXP官方网站</td>    </tr>    <tr>        <td align="left"><a href="https://www.nxpic.org.cn/" target="_blank">https://www.nxpic.org.cn/</a></td><td align="left">NXP 官方社区</td>    </tr>    <tr>        <td align="left"><a href="https://u-boot.readthedocs.io/en/latest/" target="_blank">https://u-boot.readthedocs.io/en/latest/</a></td><td align="left">u-boot官网</td>    </tr>    <tr>        <td align="left"><a href="https://www.kernel.org/" target="_blank">https://www.kernel.org/</a></td><td align="left">linux内核官网</td>    </tr>    <tr>        <td align="center" rowspan="0">参考资料</td>        <td align="left"><a href="https://yifengyou.gitbooks.io/linux-0-12/content/" target="_blank">linux-0.12 内核完全剖析</a></td>        <td align="left"><a href="https://yifengyou.gitbooks.io/linux-0-12/content/" target="_blank">kernel-0.12 · linux-0.12 (gitbooks.io)</a></td>    </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">分类</td>        <td align="center">网址</td>        <td align="center">说明</td>    </tr>    <tr>        <td align="center" rowspan="4">NXP</td>        <td align="left"><a href="https://github.com/nxp-imx" target="_blank">https://github.com/nxp-imx</a></td>        <td align="left">NXP imx开发资源GitHub组织，里边会有u-boot和linux内核的仓库</td>    </tr>    <tr>        <td align="left"><a href="https://elixir.bootlin.com/linux/latest/source" target="_blank">https://elixir.bootlin.com/linux/latest/source</a></td>        <td align="left">在线阅读linux kernel源码</td>    </tr>    <tr>        <td align="left"><a href="https://github.com/nxp-imx/linux-imx/releases/tag/rel_imx_4.1.15_2.1.0_ga" target="_blank">nxp-imx/linux-imx/releases/tag/rel_imx_4.1.15_2.1.0_ga</a></td>        <td align="left">NXP linux内核仓库tags中的rel_imx_4.1.15_2.1.0_ga</td>    </tr>    <tr>        <td align="left"><a href="https://github.com/nxp-imx/uboot-imx/releases/tag/rel_imx_4.1.15_2.1.0_ga" target="_blank">nxp-imx/uboot-imx/releases/tag/rel_imx_4.1.15_2.1.0_ga</a></td>        <td align="left">NXP u-boot仓库tags中的rel_imx_4.1.15_2.1.0_ga</td>    </tr>    <tr>        <td align="center" rowspan="2">I.MX6ULL</td>        <td align="left"><a href="https://www.nxp.com.cn/docs/en/data-sheet/IMX6ULLIEC.pdf" target="_blank">i.MX 6ULL Applications Processors for Industrial Products</a></td>        <td align="left">I.MX6ULL 芯片手册（datasheet，可以在线查看）</td>    </tr>    <tr>        <td align="left"><a href="https://www.nxp.com.cn/webapp/Download?colCode=IMX6ULLRM&lang_cd=zh" target="_blank">i.MX 6ULL Applications ProcessorReference Manual</a></td>        <td align="left">I.MX6ULL 参考手册（下载后才能查看，需要登录NXP官网）</td>    </tr>    <tr>        <td align="center" rowspan="0">Linux 0.12</td>        <td align="left"><a href="https://github.com/oldlinux-web/oldlinux-files" target="_blank">oldlinux-web/oldlinux-files</a></td>        <td align="left"><a href="https://github.com/oldlinux-web/oldlinux-files/tree/master/Linux-0.12" target="_blank">oldlinux-files/Linux-0.12 at master · oldlinux-web/oldlinux-files (github.com)</a></td>    </tr></table>
              </div>
            </details>

<h1 id="一、嵌入式Linux组成"><a href="#一、嵌入式Linux组成" class="headerlink" title="一、嵌入式Linux组成"></a><font size=3>一、嵌入式Linux组成</font></h1><p>其实应用开发应该是最先学习的部分，它很多东西其实可以不依赖于arm开发板，我们直接在自己的虚拟机里面就能运行。嵌入式Linux包含哪些东西？嵌入式 Linux 系统，就相当于一套完整的 PC 软件系统。  </p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV03-%E5%BA%94%E7%94%A8%E5%BC%80%E5%8F%91/LV01-01-%E5%BA%94%E7%94%A8%E7%BC%96%E7%A8%8B%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5-01-%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/image-20240522072123250.png" alt="image-20240522072123250" style="zoom: 33%;" />

<p>很多人喜欢从系统启动流程开始学习：先学习裸机，裸机集合起来就是 uboot，再学习内核移植、驱动开发，接下来学习根文件系统，最后学习 APP 开发。但实际开发过程中就会发现uboot其实基本不用改，而且uboot比驱动开发还复杂。这里先学习上层的app开发，也就是应用层的开发，入门相对简单。后续再去学习更深入的东西。</p>
<h1 id="二、系统调用"><a href="#二、系统调用" class="headerlink" title="二、系统调用"></a><font size=3>二、系统调用</font></h1><h2 id="1-内核态与用户态"><a href="#1-内核态与用户态" class="headerlink" title="1. 内核态与用户态"></a><font size=3>1. 内核态与用户态</font></h2><p>早期工程师们在操作系统上编写程序的时候，<strong>自己写个程序可以访问别人的程序地址，甚至是操作系统占用的地址</strong>，这样就很容易一不小心就直接把操作系统给搞挂了，所以那个时候的程序员编写程序都得小心翼翼的。</p>
<p>计算机核心的资源一般有：内存，I&#x2F;O端口，特殊机器指令等，这些资源必须得保护起来，规定哪些程序可以去访问，哪些程序不能去访问。所以引入了<strong>特权级别</strong>的概念，由硬件设备商直接来提供硬件级别的支持，最常见的就是给<strong>CPU指令集的权限分级</strong>来控制CPU的访问权限。比如 <code>Intel CPU</code>指令集操作的权限由高到低划为4级：Ring0、Ring1、Ring2、Ring3，其中Ring0权限最高，可以使用所有CPU指令集，Ring3权限最低，仅能使用部分CPU指令，比如不能使用操作硬件资源的CPU指令：I&#x2F;O操作、内存分配等操作；另外CPU处于Ring3状态不能访问Ring0的地址空间，包括<strong>代码和数据</strong>。</p>
<blockquote>
<p>CPU指令集，就是CPU中用来计算和控制计算机系统的一套指令的集合，实现软件指挥硬件执行的媒介，常见的CPU指令集有X86、ARM、MIPS、Alpha、RISC等。</p>
</blockquote>
<p>那么CPU是如何记录这些特权级信息的？</p>
<p>我们这里以<code>80386CPU</code>为例，我们知道CPU里面有许多段寄存器（CS、DS、SS、ES、FS、GS等）。这些段寄存器里面存放<strong>段选择符</strong>(也叫段选择子)：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV03-%E5%BA%94%E7%94%A8%E5%BC%80%E5%8F%91/LV01-01-%E5%BA%94%E7%94%A8%E7%BC%96%E7%A8%8B%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5-01-%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/640-1712705584672-8.webp" alt="图片" />

<p>段选择符中包含请求特权级RPL(CPL)字段，通过段选择符可以去查找全局描述符表GDT、局部描述符表LDT中对应的项，需要先进行<strong>特权级检查</strong>；这些项中都包含<strong>DPL</strong>字段(规定访问该段的权限级别)，只有<code>DPL &gt;= max &#123;CPL, RPL&#125;</code>，才允许访问。</p>
<blockquote>
<p>CPL很特殊，跟踪当前CPU正在执行的代码所在段的描述符中DPL的值，总是等于CPU的当前特权级.</p>
</blockquote>
<p><strong>内核态与用户态</strong>都是操作系统的层面的概念，和CPU硬件没有必然的联系；由于硬件已经提供了一套特权级使用的相关机制，Linux操作系统没有必要重新”造轮子”，直接使用了硬件的<code>Ring0和Ring3</code>这两个级别的权限，也就是<strong>使用Ring3作为用户态，Ring0作为内核态</strong>。</p>
<p>那么为什么Linux系统仅使用了<code>Ring0和Ring3</code>这两个级别？</p>
<p>因为CPU给的权限管理细度不够，比如<code>Intel CPU</code>中<code>Ring2</code>和<code>Ring3</code>在操作系统里安全情况没有区别，<code>Ring1</code>下的系统权限又需要经常调用<code>Ring0</code>特权指令，频繁切换特权级成本过高，操作系统不如将<code>Ring2</code>合并到<code>Ring3</code>，将<code>Ring1</code>划入<code>Ring0</code>特权级。另一方面不是每种处理器都像<code>x86</code>一样支持4个权限级别，有些处理器可能只支持2个级别，更少的特权级别，便于移植其他处理器架构上。我们再来看下linux的体系架构图：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV03-%E5%BA%94%E7%94%A8%E5%BC%80%E5%8F%91/LV01-01-%E5%BA%94%E7%94%A8%E7%BC%96%E7%A8%8B%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5-01-%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/640-1712620170595-3.webp" alt="图片" style="zoom:80%;" />

<p>我们可以发现Linux系统从整体上看，被划分为<strong>用户态和内核态</strong>：</p>
<ul>
<li>内核态</li>
</ul>
<p>内核态是处于操作系统的最核心处，<code>Ring0</code>特权级，拥有操作系统的最高权限，能够控制所有的硬件资源，掌控各种核心数据，并且能够访问内存中的任意地址;由内核态统一管理这些核心资源，减少有限资源的访问和使用冲突；在内核里发生的任何程序异常都是灾难性的，会导致整个操作系统的崩溃。</p>
<ul>
<li>用户态</li>
</ul>
<p>用户态，就是我们通常编写程序的地方，处于<code>Ring3</code>特权级，权限较低；这一层次的程序<strong>没有对硬件的直接控制权限，也不能直接访问地址的内存</strong>。在这种模式下，即使程序发生崩溃也不会影响其他程序，可恢复。</p>
<h2 id="2-什么是系统调用？"><a href="#2-什么是系统调用？" class="headerlink" title="2. 什么是系统调用？"></a><font size=3>2. 什么是系统调用？</font></h2><p>当计算机启动的时候，CPU处于Ring0状态，这个时候所有的指令都可以执行，通过主引导程序将磁盘扇区中的操作系统程序加载到内存中，从而启动操作系统(<strong>需要注意一下，本文的操作系统 以Linux0.12为例子</strong>)。</p>
<p>当<strong>Linux0.12启动</strong>的时候，是在权限最高级别的<strong>内核态</strong>运行的；同时对内存进行划分，划出一部分(内核区)专门给内核使用，这部分内存只能被内核使用；主内存区域给其他应用软件使用。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV03-%E5%BA%94%E7%94%A8%E5%BC%80%E5%8F%91/LV01-01-%E5%BA%94%E7%94%A8%E7%BC%96%E7%A8%8B%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5-01-%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/640.png" alt="图片" />

<p>当操作系统启动完成后，CPU就切换到<code>Ring3</code>级别上，操作系统同时进入用户态，之后的<strong>应用程序代码</strong>都运行在权限最低级别的<strong>用户态</strong>上，通常我们能编写的程序都运行在用户态上。</p>
<p>需要格外注意一下，<strong>CPU特权级其实并不会对操作系统的用户造成什么影响</strong>！我们可能会和Linux的用户权限搞混淆，无论是根用户(root)，管理员，访客还是一般用户，它们都属于用户；<strong>而所有的用户代码都在用户态Ring3上执行，所有的内核代码都在内核态Ring0上执行，和Linux用户的身份权限并没有关系</strong>！</p>
<p>因为我们编写的程序都运行在用户态上，是无法对内存和I&#x2F;O端口的访问，可以说基本上无法与外部世界交互，但是我们平时工作的时候访问磁盘、写文件，这些都是必要的需求，怎么办？</p>
<p>那就需要通过执行<strong>系统调用（system call）</strong>，操作系统会切换到内核态，由内核去统一执行相关操作；当执行完操作系统再切换回用户态。这样方便集中管理，减少有限资源的访问和使用冲突。</p>
<p><strong>系统调用</strong>是操作系统专门为用户态运行的进程与硬件设备之间进行交互提供了一组接口（API），是用户态<strong>主动</strong>要求切换到内核态的一种方式， 是 Linux 应用层进入内核的入口。不止 Linux 系统，所有的操作系统都会向应用层提供系统调用，应用程序通过系统调用来使用操作系统提供的各种服务。</p>
<p>通过系统调用， Linux 应用程序可以请求内核以自己的名义执行某些事情，例如打开磁盘中的文件、读写文件、关闭文件以及控制其它硬件外设。通过系统调用 API，应用层可以实现与内核的交互，其关系可通过下图简单描述：  </p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV03-%E5%BA%94%E7%94%A8%E5%BC%80%E5%8F%91/LV01-01-%E5%BA%94%E7%94%A8%E7%BC%96%E7%A8%8B%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5-01-%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/image-20240403073828181.png" alt="image-20240403073828181" style="zoom:50%;" />

<p>内核提供了一系列的服务、资源、支持一系列功能，应用程序通过调用系统调用 API 函数来使用内核提供的服务、资源以及各种各样的功能。</p>
<h2 id="3-为什么要有系统调用？"><a href="#3-为什么要有系统调用？" class="headerlink" title="3. 为什么要有系统调用？"></a><font size=3>3. 为什么要有系统调用？</font></h2><p>其实前面学习系统调用概念的时候有提过，主要是计算机的各种<strong>硬件资源是有限的</strong>，为了更好的管理这些资源，<strong>用户进程是不允许直接操作的</strong>，所有对这些资源的访问都必须由操作系统控制，也就是说操作系统是使用这些资源的唯一入口。为此<strong>操作系统为用户态运行的进程与硬件设备之间进行交互提供了一组接口，这组接口就是所谓的系统调用。</strong></p>
<blockquote>
<p>在应用程序和硬件之间设置这样一个接口层有什么优点呢？</p>
<ul>
<li><p>把用户从学习硬件设备的低级编程特性中解放出来。</p>
</li>
<li><p>系统调用保证了系统的稳定和安全。作为硬件设备和应用程序之间的中间环节，内核可以基于权限和其他一些规则对需要进行的访问进行裁决。比如这样就可以避免应用程序不正确地使用硬件设备，窃取其他进程的资源，或做出其他什么危害系统的事情。</p>
</li>
<li><p>最重要的是，这些接口使得程序更具有可移植性，因为只要不同操作系统所提供的一组接口相同，那么在这些操作系统上就可以正确的编译和执行相同的程序。</p>
</li>
</ul>
</blockquote>
<h2 id="4-系统调用怎么实现的？"><a href="#4-系统调用怎么实现的？" class="headerlink" title="4. 系统调用怎么实现的？"></a><font size=3>4. 系统调用怎么实现的？</font></h2><h3 id="4-1-strace命令"><a href="#4-1-strace命令" class="headerlink" title="4.1 strace命令"></a><font size=3>4.1 strace命令</font></h3><p>在Linux系统中，strace命令是一个集诊断、调试、统计与一体的工具，可用来追踪调试程序，能够与其他命令搭配使用。Linux系统管理员可以在不需要源代码的情况下即可跟踪系统的调用。官网在这里：<a target="_blank" rel="noopener" href="https://strace.io/">strace</a></p>
<p>strace命令会显示有关进程的系统调用的信息，这可以帮助确定一个程序使用的哪个函数，当然在系统出现问题时可以使用 strace定位系统调用过程中失败的原因，这是定位系统问题的很好的方法。这里只是简单提一下，知道有这么个工具，后续排查问题可能会比较有效。</p>
<p>怎么安装移植？可以看这篇笔记：01嵌入式开发&#x2F;02IMX6ULL平台&#x2F;LV03-应用开发&#x2F;《LV07-04-strace命令-移植》</p>
<h3 id="4-2-以库函数write为例"><a href="#4-2-以库函数write为例" class="headerlink" title="4.2 以库函数write为例"></a><font size=3>4.2 以库函数write为例</font></h3><p>啊，没找到源码，参考下这个吧：<a target="_blank" rel="noopener" href="https://github.com/sumumm/linux-0.12/blob/master/linux-0.12/lib/write.c">linux-0.12&#x2F;linux-0.12&#x2F;lib&#x2F;write.c at master · sumumm&#x2F;linux-0.12 (github.com)</a>：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> *  linux/lib/write.c</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> *  (C) 1991  Linus Torvalds</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> __LIBRARY__</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;unistd.h&gt;</span></span></span><br><span class="line"></span><br><span class="line">_syscall3(<span class="type">int</span>,write,<span class="type">int</span>,fd,<span class="type">const</span> <span class="type">char</span> *,buf,<span class="type">off_t</span>,count)</span><br></pre></td></tr></table></figure>

<p><code>write.c</code>这个文件主要是定义write的实现，<code>_syscall3(*,write,*)</code>函数的主要功能是，向文件描述符fd指定的文件写入count个字节的数据到缓冲区buf中</p>
<p>需要注意一下<code>#define __LIBRARY__</code>这个宏定义，这里定义<strong>直接原因</strong>是为了包括在<code>unistd.h</code>中的内嵌汇编代码。</p>
<h3 id="4-3-库函数扩展汇编宏"><a href="#4-3-库函数扩展汇编宏" class="headerlink" title="4.3 库函数扩展汇编宏"></a><font size=3>4.3 库函数扩展汇编宏</font></h3><p>因为<code>_syscall3</code>这个函数定义在<code>/include/unistd.h</code>中，来看下源码（<a target="_blank" rel="noopener" href="https://github.com/sumumm/linux-0.12/blob/master/linux-0.12/include/unistd.h">linux-0.12&#x2F;linux-0.12&#x2F;include&#x2F;unistd.h at master · sumumm&#x2F;linux-0.12 (github.com)</a>）：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  /include/unistd.h</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">ifdef</span> __LIBRARY__ # 若提前定义__LIBRARY__，则以后内容被包含</span></span><br><span class="line"></span><br><span class="line">...</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> __NR_write 4 <span class="comment">//系统调用号，用作系统调用函数表中索引值</span></span></span><br><span class="line"></span><br><span class="line">...</span><br><span class="line"></span><br><span class="line"><span class="comment">//定义有3个參数的, 定义系统调用嵌入式汇编宏函数</span></span><br><span class="line"><span class="comment">//%0 - eax(__res)，%1 - eax(__NR_name)，%2 - ebx(a)，%3 - ecx(b)，%4 - edx(c)。</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> _syscall3(type,name,atype,a,btype,b,ctype,c) \</span></span><br><span class="line"><span class="meta">type name(atype a,btype b,ctype c) \</span></span><br><span class="line"><span class="meta">&#123; \</span></span><br><span class="line"><span class="meta">long __res; \</span></span><br><span class="line"><span class="meta">__asm__ volatile (<span class="string">&quot;int $0x80&quot;</span> \                                             <span class="comment">// 调用系统中断 0x80</span></span></span><br><span class="line"> : <span class="string">&quot;=a&quot;</span> (__res) \                                                          <span class="comment">// 返回值eax(__res)</span></span><br><span class="line"> : <span class="string">&quot;0&quot;</span> (__NR_#<span class="meta">#name),<span class="string">&quot;b&quot;</span> ((long)(a)),<span class="string">&quot;c&quot;</span> ((long)(b)),<span class="string">&quot;d&quot;</span> ((long)(c))); \   <span class="comment">//输入为：系统中断调用号__NR_name,还有另外3个参数</span></span></span><br><span class="line"><span class="keyword">if</span> (__res&gt;=<span class="number">0</span>) \                                                             <span class="comment">// 如果返回值&gt;=0，则直接返回该值</span></span><br><span class="line"> <span class="keyword">return</span> (type) __res; \</span><br><span class="line">errno=-__res; \                                                             <span class="comment">// 否则置出错号，并返回-1</span></span><br><span class="line"><span class="keyword">return</span> <span class="number">-1</span>; \                                                                </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span> <span class="comment">/* __LIBRARY__ */</span></span></span><br><span class="line"></span><br><span class="line">...</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> write(<span class="type">int</span> fildes, <span class="type">const</span> <span class="type">char</span> * buf, <span class="type">off_t</span> count); <span class="comment">//write系统调用的函数原型定义</span></span><br><span class="line"></span><br><span class="line">...</span><br></pre></td></tr></table></figure>

<p>只有在<code>lib/write.c</code>中先定义了<code>#define __LIBRARY__</code>，那么才能在<code>/include/unistd.h</code>中，找到系统调用号和内嵌汇编<code>_syscall3()</code>；不然就代表它不需要进行系统调用，这样就可以忽略<code>unistd.h</code>中和系统调用相关的宏定义。其实我们可以把write.c中的write函数再重新整合一下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">write</span><span class="params">(<span class="type">int</span> fd,<span class="type">const</span> <span class="type">char</span>* buf,<span class="type">off_t</span> count)</span> \</span><br><span class="line">&#123; \</span><br><span class="line"><span class="type">long</span> __res; \</span><br><span class="line">__asm__ <span class="title function_">volatile</span> <span class="params">( <span class="string">&quot;int $0x80&quot;</span> \</span></span><br><span class="line"><span class="params">: <span class="string">&quot;=a&quot;</span> (__res) \</span></span><br><span class="line"><span class="params">: <span class="string">&quot;&quot;</span> (__NR_write), <span class="string">&quot;b&quot;</span> ((<span class="type">long</span>)(fd)), <span class="string">&quot;c&quot;</span> ((<span class="type">long</span>)(buf)), <span class="string">&quot;d&quot;</span> ((<span class="type">long</span>)(count)))</span>; \</span><br><span class="line"><span class="keyword">if</span> (__res&gt;=<span class="number">0</span>) \</span><br><span class="line"><span class="keyword">return</span> (type) __res; \</span><br><span class="line">errno=-__res; \</span><br><span class="line"><span class="keyword">return</span> <span class="number">-1</span>; \</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这样就能更容易明白<code>#define __LIBRARY__</code>的作用。上面<code>int $0x80&quot;</code>表示调用<strong>系统中断0x80 ** ，其实</strong>系统调用的本质还是通过中断(0x80)去实现的**！</p>
<p>另外由于程序处于用户态无法直接操作硬件资源，所以需要进行<strong>系统调用</strong>，切换到内核态；也就是说用户程序如果使用库函数<code>write</code>，会进行系统调用。而系统调用，其实就是去调用<code>int 0x80</code>中断，然后把三个参数<code>fd、buf、count</code>依次存入<code>ebx、ecx、edx</code>寄存器。还有<code>#define __NR_write 4</code> ，定义了<strong>系统调用号</strong>；<code>_NR_write</code>会被存入<code>eax</code>寄存器；当调用返回后，从<code>eax</code>取出返回值，存入<code>__res</code>，建立了用户栈和内核栈的联系。至于<code>__NR_write</code>的作用后面再学习。</p>
<h3 id="4-4-int-0x80中断-调用对应的中断处理函数"><a href="#4-4-int-0x80中断-调用对应的中断处理函数" class="headerlink" title="4.4 int 0x80中断 调用对应的中断处理函数"></a><font size=3>4.4 int 0x80中断 调用对应的中断处理函数</font></h3><p>我们来看下中断是调用对应的中断处理函数的流程图：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV03-%E5%BA%94%E7%94%A8%E5%BC%80%E5%8F%91/LV01-01-%E5%BA%94%E7%94%A8%E7%BC%96%E7%A8%8B%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5-01-%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/641.png" alt="图片"  />

<p>当发生中断的时候，CPU获取到中断向量号后，通过<code>IDTR</code>，去查找<code>IDT</code>中断描述符表，得到相应的中断描述符；然后根据描述符中的对应中断处理程序的入口地址，去执行中断处理程序.</p>
<p>早在linux0.12启动时，会进行调度程序初始化<code>main.c/sched_init()</code>，其源码（<a target="_blank" rel="noopener" href="https://github.com/sumumm/linux-0.12/blob/master/linux-0.12/kernel/sched.c">linux-0.12&#x2F;linux-0.12&#x2F;kernel&#x2F;sched.c at master · sumumm&#x2F;linux-0.12 (github.com)</a>）：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//     /kernel/sched.c</span></span><br><span class="line"></span><br><span class="line">...</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">sched_init</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line"> ...</span><br><span class="line"> set_system_gate(<span class="number">0x80</span>,&amp;system_call);<span class="comment">//设置系统调用中断门</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">...</span><br></pre></td></tr></table></figure>

<p>需要注意的是：在用户态和内核态运行的进程使用的栈是不同的，分别叫做<strong>用户栈和内核栈</strong>， 两者各自负责相应特权级别状态下的函数调用；所以当执行系统调用中断<code>int 0x80</code>从用户态进入内核态时，会<strong>从用户栈切换到内核栈</strong>，系统调用返回时，还要切换回用户栈，继续完成用户态下的函数调用(这也叫做被中断进程上下文的保存与恢复)。</p>
<p>其中其关键作用的是，CPU会可以自动通过<code>TR</code>寄存器找到当前进程的<code>TSS</code>，然后根据里面<code>ss0</code>和<code>esp0</code>的值找到内核栈的位置，完成用户栈到内核栈的切换。</p>
<p><code>set_system_gate(0x80,&amp;system_call)</code>这句整体作用是，<strong>设置系统调用中断门</strong>，将<code>0x80</code>中断和函数<code>system_call</code>绑定在一起，换句话说<code>system_call</code>就是<code>0x80</code>的中断处理函数。</p>
<h3 id="4-5-检索系统调用函数表"><a href="#4-5-检索系统调用函数表" class="headerlink" title="4.5 检索系统调用函数表"></a><font size=3>4.5 检索系统调用函数表</font></h3><p>我们接着去看<code>system_call</code>函数的源码（<a target="_blank" rel="noopener" href="https://github.com/sumumm/linux-0.12/blob/master/linux-0.12/kernel/sys_call.s">linux-0.12&#x2F;linux-0.12&#x2F;kernel&#x2F;sys_call.s at master · sumumm&#x2F;linux-0.12 (github.com)</a>）：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">//    /kernel/sys_call.s</span><br><span class="line"></span><br><span class="line">...</span><br><span class="line"></span><br><span class="line">// int 0x80</span><br><span class="line">_system_call:</span><br><span class="line"> push %ds      # 压栈, 保存原段寄存器值</span><br><span class="line"> push %es</span><br><span class="line"> push %fs   </span><br><span class="line"> pushl %eax    # 保存eax原值</span><br><span class="line"> pushl %edx  </span><br><span class="line"> pushl %ecx    # push %ebx,%ecx,%edx as parameters</span><br><span class="line"> pushl %ebx    # to the system call,  ebx,ecx,edx 中放着系统调用对应的C语言函数的参数</span><br><span class="line"> movl $0x10,%edx  # ds,es 指向内核数据段</span><br><span class="line"> mov %dx,%ds</span><br><span class="line"> mov %dx,%es</span><br><span class="line"> movl $0x17,%edx  # fs 指向当前局部数据段(局部描述符表中数据段描述符)</span><br><span class="line"> mov %dx,%fs</span><br><span class="line"> cmpl _NR_syscalls,%eax  # 判断eax是否超过了最大的系统调用号,调用号如果超出范围的话就跳转!</span><br><span class="line"> jae bad_sys_call</span><br><span class="line"> call _sys_call_table(,%eax,4)   # 间接调用指定功能C函数!</span><br><span class="line"> pushl %eax                      #  把系统调用的返回值入栈！</span><br><span class="line"></span><br><span class="line">...</span><br><span class="line"></span><br><span class="line">ret_from_sys_call:  #当系统调用执行完毕之后，会执行此处的汇编代码，从而返回用户态</span><br><span class="line"> movl _current,%eax  # 取当前任务（进程）数据结构指针-&gt;eax</span><br><span class="line"> cmpl _task,%eax   # task[0] cannot have signals</span><br><span class="line"> ...</span><br></pre></td></tr></table></figure>

<p>其中 <code>_sys_call_table(,%eax,4)</code>，这里的<code>eax</code>寄存器存放的就是<code>_NR_write</code>系统调用号，<code>_sys_call_table</code>是sys.h中的一个<code>int (*)()</code>类型的数组，里面存的是所有的系统调用函数地址，也叫做<strong>系统调用函数表</strong>，所以<code>__NR_write</code>也表示系统调用函数表中的索引值。</p>
<p>那为什么<code>%eax * 4</code>乘上4呢？这是因为<code>sys_call_table[]</code>指针每项4 个字节，这样被调用处理函数的地址&#x3D;<code>[_sys_call_table + %eax * 4]</code>。</p>
<p>我们再来看下<code>sys_call_table</code>的定义（<a target="_blank" rel="noopener" href="https://github.com/sumumm/linux-0.12/blob/master/linux-0.12/include/linux/sys.h">linux-0.12&#x2F;linux-0.12&#x2F;include&#x2F;linux&#x2F;sys.h at master · sumumm&#x2F;linux-0.12 (github.com)</a>）：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//    /include/linux/sys.h</span></span><br><span class="line"></span><br><span class="line">...</span><br><span class="line"><span class="keyword">extern</span> <span class="type">int</span> <span class="title function_">sys_write</span><span class="params">()</span>;</span><br><span class="line">...</span><br><span class="line"></span><br><span class="line">fn_ptr sys_call_table[] = &#123; sys_setup, sys_exit, sys_fork, sys_read,</span><br><span class="line">sys_write, sys_open, sys_close, sys_waitpid, sys_creat, sys_link,</span><br><span class="line">sys_unlink, sys_execve, sys_chdir, sys_time, sys_mknod, sys_chmod,</span><br><span class="line">sys_chown, sys_break, sys_stat, sys_lseek, sys_getpid, sys_mount,</span><br><span class="line">sys_umount, sys_setuid, sys_getuid, sys_stime, sys_ptrace, sys_alarm,</span><br><span class="line">sys_fstat, sys_pause, sys_utime, sys_stty, sys_gtty, sys_access,</span><br><span class="line">sys_nice, sys_ftime, sys_sync, sys_kill, sys_rename, sys_mkdir,</span><br><span class="line">sys_rmdir, sys_dup, sys_pipe, sys_times, sys_prof, sys_brk, sys_setgid,</span><br><span class="line">sys_getgid, sys_signal, sys_geteuid, sys_getegid, sys_acct, sys_phys,</span><br><span class="line">sys_lock, sys_ioctl, sys_fcntl, sys_mpx, sys_setpgid, sys_ulimit,</span><br><span class="line">sys_uname, sys_umask, sys_chroot, sys_ustat, sys_dup2, sys_getppid,</span><br><span class="line">sys_getpgrp, sys_setsid, sys_sigaction, sys_sgetmask, sys_ssetmask,</span><br><span class="line">sys_setreuid,sys_setregid, sys_sigsuspend, sys_sigpending, sys_sethostname,</span><br><span class="line">sys_setrlimit, sys_getrlimit, sys_getrusage, sys_gettimeofday, </span><br><span class="line">sys_settimeofday, sys_getgroups, sys_setgroups, sys_select, sys_symlink,</span><br><span class="line">sys_lstat, sys_readlink, sys_uselib &#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">//系统调用总数目,注意一下：这里相较于linux0.11做了改进，新增系统调用不再需要手动调整该数目！</span></span><br><span class="line"><span class="type">int</span> NR_syscalls = <span class="keyword">sizeof</span>(sys_call_table)/<span class="keyword">sizeof</span>(fn_ptr);</span><br></pre></td></tr></table></figure>

<p>可以知晓这里的<code>call _sys_call_table(,%eax,4)</code>就是调用系统调用号所对应的内核系统调用函数<code>sys_write</code>。</p>
<h3 id="4-6-最终执行sys-write"><a href="#4-6-最终执行sys-write" class="headerlink" title="4.6 最终执行sys_write"></a><font size=3>4.6 最终执行sys_write</font></h3><p><code>sys_write</code>在fs下的<code>read_write.c</code>（<a target="_blank" rel="noopener" href="https://github.com/sumumm/linux-0.12/blob/master/linux-0.12/fs/read_write.c">linux-0.12&#x2F;linux-0.12&#x2F;fs&#x2F;read_write.c at master · sumumm&#x2F;linux-0.12 (github.com)</a>）：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//   /fs/read_write.c</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 写文件系统调用</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">sys_write</span><span class="params">(<span class="type">unsigned</span> <span class="type">int</span> fd,<span class="type">char</span> * buf,<span class="type">int</span> count)</span></span><br><span class="line">&#123;</span><br><span class="line"> <span class="class"><span class="keyword">struct</span> <span class="title">file</span> * <span class="title">file</span>;</span></span><br><span class="line"> <span class="class"><span class="keyword">struct</span> <span class="title">m_inode</span> * <span class="title">inode</span>;</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">//判断函数参数的有效性</span></span><br><span class="line"> <span class="keyword">if</span> (fd&gt;=NR_OPEN || count &lt;<span class="number">0</span> || !(file=current-&gt;filp[fd]))</span><br><span class="line">  <span class="keyword">return</span> -EINVAL;</span><br><span class="line"> <span class="keyword">if</span> (!count)</span><br><span class="line">  <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">  <span class="comment">// 取文件相应的i节点</span></span><br><span class="line"> inode=file-&gt;f_inode;</span><br><span class="line">  <span class="comment">// 若是管道文件，并且是写管道文件模式，则进行写管道操作</span></span><br><span class="line"> <span class="keyword">if</span> (inode-&gt;i_pipe)</span><br><span class="line">  <span class="keyword">return</span> (file-&gt;f_mode&amp;<span class="number">2</span>)?write_pipe(inode,buf,count):-EIO;</span><br><span class="line">  <span class="comment">//如果是字符设备文件，则进行写字符设备操作</span></span><br><span class="line"> <span class="keyword">if</span> (S_ISCHR(inode-&gt;i_mode))</span><br><span class="line">  <span class="keyword">return</span> rw_char(WRITE,inode-&gt;i_zone[<span class="number">0</span>],buf,count,&amp;file-&gt;f_pos);</span><br><span class="line">  <span class="comment">// 如果是块设备文件，则进行块设备写操作</span></span><br><span class="line"> <span class="keyword">if</span> (S_ISBLK(inode-&gt;i_mode))</span><br><span class="line">  <span class="keyword">return</span> block_write(inode-&gt;i_zone[<span class="number">0</span>],&amp;file-&gt;f_pos,buf,count);</span><br><span class="line">  <span class="comment">// 若是常规文件，则执行文件写操作</span></span><br><span class="line"> <span class="keyword">if</span> (S_ISREG(inode-&gt;i_mode))</span><br><span class="line">  <span class="keyword">return</span> file_write(inode,file,buf,count);</span><br><span class="line"> printk(<span class="string">&quot;(Write)inode-&gt;i_mode=%06o\n\r&quot;</span>,inode-&gt;i_mode);</span><br><span class="line"> <span class="keyword">return</span> -EINVAL;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>至此库函数write，进行系统调用，最终调用了<code>sys_write</code>这个函数。</p>
<h3 id="4-7-总结"><a href="#4-7-总结" class="headerlink" title="4.7 总结"></a><font size=3>4.7 总结</font></h3><img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV03-%E5%BA%94%E7%94%A8%E5%BC%80%E5%8F%91/LV01-01-%E5%BA%94%E7%94%A8%E7%BC%96%E7%A8%8B%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5-01-%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/640-1716246922738-3.webp" alt="图片" />

<h2 id="5-内核态与用户态数据交互"><a href="#5-内核态与用户态数据交互" class="headerlink" title="5. 内核态与用户态数据交互"></a><font size=3>5. 内核态与用户态数据交互</font></h2><p>到这里我们已经了解了系统调用的过程，还遗留一个问题需要去解决一下，就是内核态与用户态如何进行数据交互？回顾系统调用过程中，我们可以发现<strong>寄存器</strong>在其中起到了不可或缺的作用，linus在<code>linux0.12</code>中也是采用类似的方法来进行数据交互。</p>
<p>我们这里继续以<code>sys_write</code>函数为例，来看看里面的<code>file_write(inode,file,buf,count);</code>，<a target="_blank" rel="noopener" href="https://github.com/sumumm/linux-0.12/blob/master/linux-0.12/fs/file_dev.c">linux-0.12&#x2F;linux-0.12&#x2F;fs&#x2F;file_dev.c at master · sumumm&#x2F;linux-0.12 (github.com)</a>：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//   /fs/file_dev.c</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 写文件函数 - 根据 i 节点和文件结构信息，将用户数据写入文件中</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">file_write</span><span class="params">(<span class="keyword">struct</span> m_inode * inode, <span class="keyword">struct</span> file * filp, <span class="type">char</span> * buf, <span class="type">int</span> count)</span></span><br><span class="line">&#123;</span><br><span class="line"> <span class="type">off_t</span> pos;</span><br><span class="line"> <span class="type">int</span> block,c;</span><br><span class="line"> <span class="class"><span class="keyword">struct</span> <span class="title">buffer_head</span> * <span class="title">bh</span>;</span></span><br><span class="line"> <span class="type">char</span> * p;</span><br><span class="line"> <span class="type">int</span> i=<span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * ok, append may not work when many processes are writing at the same time</span></span><br><span class="line"><span class="comment"> * but so what. That way leads to madness anyway.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="comment">//如果设置了追加标记位，则更新当前位置指针到文件最后一个字节</span></span><br><span class="line"> <span class="keyword">if</span> (filp-&gt;f_flags &amp; O_APPEND)</span><br><span class="line">  pos = inode-&gt;i_size;</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line">  pos = filp-&gt;f_pos;</span><br><span class="line">  <span class="comment">// i为已经写入的长度，count为需要写入的长度</span></span><br><span class="line"> <span class="keyword">while</span> (i&lt;count) &#123;</span><br><span class="line">    <span class="comment">// 先取文件数据块号，如果没有则创建一个块</span></span><br><span class="line">  <span class="keyword">if</span> (!(block = create_block(inode,pos/BLOCK_SIZE)))</span><br><span class="line">   <span class="keyword">break</span>;</span><br><span class="line">  <span class="keyword">if</span> (!(bh=bread(inode-&gt;i_dev,block)))</span><br><span class="line">   <span class="keyword">break</span>;</span><br><span class="line">  c = pos % BLOCK_SIZE;</span><br><span class="line">  p = c + bh-&gt;b_data;<span class="comment">// 开始写入数据的位置</span></span><br><span class="line">  bh-&gt;b_dirt = <span class="number">1</span>; <span class="comment">//标记数据需要回写硬盘</span></span><br><span class="line">  c = BLOCK_SIZE-c; <span class="comment">//算出能写的长度</span></span><br><span class="line">  <span class="keyword">if</span> (c &gt; count-i) c = count-i;</span><br><span class="line">  pos += c;</span><br><span class="line">  <span class="keyword">if</span> (pos &gt; inode-&gt;i_size) &#123;</span><br><span class="line">   inode-&gt;i_size = pos;</span><br><span class="line">   inode-&gt;i_dirt = <span class="number">1</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  i += c;</span><br><span class="line">  <span class="keyword">while</span> (c--&gt;<span class="number">0</span>)</span><br><span class="line">   *(p++) = get_fs_byte(buf++);<span class="comment">//从用户态拷贝一个字节的数据到内核态</span></span><br><span class="line">  brelse(bh);</span><br><span class="line"> &#125;</span><br><span class="line">  <span class="comment">//当数据已经全部写入文件或者在写操作过程中发生问题时就会退出循环</span></span><br><span class="line"> inode-&gt;i_mtime = CURRENT_TIME;</span><br><span class="line"> <span class="keyword">if</span> (!(filp-&gt;f_flags &amp; O_APPEND)) &#123;</span><br><span class="line">  filp-&gt;f_pos = pos;</span><br><span class="line">  inode-&gt;i_ctime = CURRENT_TIME;</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="keyword">return</span> (i?i:<span class="number">-1</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>我们这里不详细看了，把目光聚焦于<code>get_fs_byte</code>函数，我们来看下其源码（<a target="_blank" rel="noopener" href="https://github.com/sumumm/linux-0.12/blob/master/linux-0.12/include/asm/segment.h">linux-0.12&#x2F;linux-0.12&#x2F;include&#x2F;asm&#x2F;segment.h at master · sumumm&#x2F;linux-0.12 (github.com)</a>）：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  include/asm/segment.h</span></span><br><span class="line"> </span><br><span class="line"> <span class="comment">// 读取 fs 段中指定地址处的字节。</span></span><br><span class="line"> <span class="comment">// 参数：addr - 指定的内存地址。</span></span><br><span class="line"> <span class="comment">// %0 - (返回的字节_v)；%1 - (内存地址 addr)。</span></span><br><span class="line"> <span class="comment">// 返回：返回内存 fs:[addr]处的字节。</span></span><br><span class="line"> <span class="comment">// 第 3 行定义了一个寄存器变量_v，该变量将被保存在一个寄存器中，以便于高效访问和操作。</span></span><br><span class="line"><span class="keyword">extern</span> <span class="keyword">inline</span> <span class="type">unsigned</span> <span class="type">char</span> <span class="title function_">get_fs_byte</span><span class="params">(<span class="type">const</span> <span class="type">char</span> * addr)</span></span><br><span class="line">&#123;</span><br><span class="line"> <span class="type">unsigned</span> <span class="keyword">register</span> <span class="type">char</span> _v;</span><br><span class="line"></span><br><span class="line"> __asm__ (<span class="string">&quot;movb %%fs:%1,%0&quot;</span>:<span class="string">&quot;=r&quot;</span> (_v):<span class="string">&quot;m&quot;</span> (*addr));</span><br><span class="line"> <span class="keyword">return</span> _v;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 将一字节存放在 fs 段中指定内存地址处。</span></span><br><span class="line"> <span class="comment">// 参数：val - 字节值；addr - 内存地址。</span></span><br><span class="line"> <span class="comment">// %0 - 寄存器(字节值 val)；%1 - (内存地址 addr)。</span></span><br><span class="line"><span class="keyword">extern</span> <span class="keyword">inline</span> <span class="type">void</span> <span class="title function_">put_fs_byte</span><span class="params">(<span class="type">char</span> val,<span class="type">char</span> *addr)</span></span><br><span class="line">&#123;</span><br><span class="line">__asm__ (<span class="string">&quot;movb %0,%%fs:%1&quot;</span>::<span class="string">&quot;r&quot;</span> (val),<span class="string">&quot;m&quot;</span> (*addr));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><code>get_fs_byte</code>函数是从用户态拷贝一个字节的数据到内核态，而<code>put_fs_byte</code>则恰恰相反，从内核态拷贝一个字节的数据到用户态。在系统调用运行整个过程中，<strong>DS和ES段寄存器指向内核数据空间，而FS段寄存器被设置为指向用户数据空间</strong>，这为啥？我们来看在<code>/kernel/sys_call.s</code>（<a target="_blank" rel="noopener" href="https://github.com/sumumm/linux-0.12/blob/master/linux-0.12/kernel/sys_call.s">linux-0.12&#x2F;linux-0.12&#x2F;kernel&#x2F;sys_call.s at master · sumumm&#x2F;linux-0.12 (github.com)</a>）中<code>_system_call</code>中的这段：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">_system_call:</span><br><span class="line">...</span><br><span class="line"> movl $0x10,%edx  # ds,es 指向内核数据段</span><br><span class="line"> mov %dx,%ds</span><br><span class="line"> mov %dx,%es</span><br><span class="line"> movl $0x17,%edx  # fs 指向当前局部数据段(局部描述符表中数据段描述符)</span><br><span class="line"> mov %dx,%fs</span><br><span class="line">...</span><br></pre></td></tr></table></figure>

<p><code>0x10</code>是全局描述符表GDT中<strong>内核数据段描述符</strong>的段值，<code>0x17</code>是<strong>局部描述符表LDT中的任务的数据段描述符</strong>的段值，所以<code>linux</code>这里利用<code>FS</code>寄存器来完成<code>内核数据空</code>间与<code>用户数据空间</code>之间的数据复制，当进程从中断调用中退出时，寄存器会自动从内核栈弹出，快捷高效。</p>
<h1 id="三、库函数"><a href="#三、库函数" class="headerlink" title="三、库函数"></a><font size=3>三、库函数</font></h1><p>系统调用是内核直接向应用层提供的应用编程接口， 譬如 open、 write、read、 close 等，关于这些系统调用后面学习。 编写应用程序除了使用系统调用之外，我们还可以使用库函数。</p>
<p>库函数也就是 C 语言库函数， C 语言库是应用层使用的一套函数库， 在 Linux 下，通常以动态（.so）库文件的形式提供，存放在根文件系统&#x2F;lib目录下， C 语言库函数构建于系统调用之上，也就是说库函数其实是由系统调用封装而来的，当然也不能完全这么说， 原因在于有些库函数并不调用任何系统调用，例如一些字符串处理函数 strlen()、 strcat()、 memcpy()、 memset()、 strchr()等等； 而有些库函数则会使用系统调用来帮它完成实际的操作，如库函数 fopen 内部调用了系统调用 open()来帮它打开文件、库函数 fread()就利用了系统调用 read()来完成读文件操作、 fwrite()就利用了系统调用 write()来完成写文件操作。  </p>
<p>Linux 系统内核提供了一系列的系统调用供应用层使用， 我们直接使用系统调用就可以了呀，那为何还要设计出库函数呢？事实上， 有些系统调用使用起来并不是很方便， 于是就出现了 C 语言库，这些 C 语言库函数的设计是为了提供比底层系统调用更为方便、更为好用、 且更具有可移植性的调用接口。  </p>
<blockquote>
<p>库函数与系统调用</p>
<ul>
<li>库函数是属于应用层，而系统调用是内核提供给应用层的编程接口，属于系统内核的一部分；</li>
<li>库函数运行在用户空间，调用系统调用会由用户空间（用户态）陷入到内核空间（内核态）；</li>
<li>库函数通常是有缓存的，而系统调用是无缓存的，所以在性能、效率上，库函数通常要优于系统调用；</li>
<li>可移植性：库函数相比于系统调用具有更好的可移植性，通常对于不同的操作系统，其内核向应用层提供的系统调用往往都是不同，譬如系统调用的定义、功能、参数列表、返回值等往往都是不一样的；而对于 C 语言库函数来说，由于很多操作系统都实现了 C 语言库， C 语言库在不同的操作系统之间其接口定义几乎是一样的，所以库函数在不同操作系统之间相比于系统调用具有更好的可移植性。</li>
</ul>
</blockquote>
<p>从实现者的角度来看，系统调用与库函数之间有根本的区别，但从用户使用角度来看，其区别并不重要，它们都是 C 语言函数。 在实际应用编程中，库函数和系统调用都会使用到，所以对于我们来说，直接把它们当做是 C 函数即可，知道自己调用的函数是系统调用还是库函数即可，不用太过于区分它们之间的差别。所以应用编程简单点来说就是：开发 Linux 应用程序，通过调用内核提供的系统调用或使用 C 库函数来开发具有相应功能的应用程序。  </p>
<h1 id="四、标准C语言库函数"><a href="#四、标准C语言库函数" class="headerlink" title="四、标准C语言库函数"></a><font size=3>四、标准C语言库函数</font></h1><h2 id="1-GNU-C库"><a href="#1-GNU-C库" class="headerlink" title="1. GNU C库"></a><font size=3>1. GNU C库</font></h2><p>在 Linux 系统下，使用的 C 语言库为 GNU C 语言函数库（也叫作 glibc ，其网址为 <a target="_blank" rel="noopener" href="https://www.gnu.org/software/libc/">The GNU C Library - GNU Project - Free Software Foundation</a>），作为 Linux 下的标准 C 语言函数库。  我们点开官网，就可以看到下载的地方：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV03-%E5%BA%94%E7%94%A8%E5%BC%80%E5%8F%91/LV01-01-%E5%BA%94%E7%94%A8%E7%BC%96%E7%A8%8B%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5-01-%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/image-20240521073946105.png" alt="image-20240521073946105" style="zoom: 25%;" />

<h2 id="2-glibc版本？"><a href="#2-glibc版本？" class="headerlink" title="2. glibc版本？"></a><font size=3>2. glibc版本？</font></h2><p>我们怎么确定 Linux 系统的 glibc 版本 ？前面提到过了， C 语言库是以动态库文件的形式提供的，通常存放在&#x2F;lib 目录，它的命名方式通常是<br>libc.so.6，不过这个是一个软链接文件，它会链接到真正的库文件。  </p>
<p>进入到 Ubuntu 系统的&#x2F;lib 目录下，我使用的 Ubuntu 版本为 20.04，在我的&#x2F;lib 目录下并没有发现libc.so.6 这个文件， 其实是在&#x2F;lib&#x2F;x86_64-linux-gnu 目录下，进入到该目录：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV03-%E5%BA%94%E7%94%A8%E5%BC%80%E5%8F%91/LV01-01-%E5%BA%94%E7%94%A8%E7%BC%96%E7%A8%8B%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5-01-%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/image-20240521075140856.png" alt="image-20240521075140856" style="zoom:50%;" />

<p>可以看到 libc.so.6 链接到了 libc-2.23.so 库文件， 2.31表示的就是这个 glibc 库的版本号为 2.31。除此之外，我们还可以直接运行该共享库来获取到它的信息，如下所示：  </p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV03-%E5%BA%94%E7%94%A8%E5%BC%80%E5%8F%91/LV01-01-%E5%BA%94%E7%94%A8%E7%BC%96%E7%A8%8B%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5-01-%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/img/image-20240521075233548.png" alt="image-20240521075233548" style="zoom:50%;" />

<p>从打印信息可以看到， 我所使用的 Ubuntu 系统对应的 glibc 版本号为 2.31。  </p>

    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/9baa712e.html">LV01-01-应用编程基本概念-01-基础知识</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2024年11月08日 - 18:50</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/9baa712e.html" title="LV01-01-应用编程基本概念-01-基础知识">https://sumumm.github.io/post/9baa712e.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/ALPHA-LV03-%E5%BA%94%E7%94%A8%E5%BC%80%E5%8F%91/" rel="tag"><i class="fa fa-tag"></i> (ALPHA)LV03-应用开发</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/2a85f40a.html" rel="prev" title="LV04-02-GPIO-01-GPIO简介">
                  <i class="fa fa-angle-left"></i> LV04-02-GPIO-01-GPIO简介
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/8b41a690.html" rel="next" title="LV04-00-IMX6ULL-01-内存映射">
                  LV04-00-IMX6ULL-01-内存映射 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
